import {Component, OnInit} from '@angular/core';
import {FormControl, FormGroup} from "@angular/forms";
import {BarnProduct} from "../../product/entity/barn-product";
import {BarnDocument} from "../entity/barn-document";
import {Constants} from "../../../../../tools";
import {ProductService} from "../../product/service/product.service";
import {BarnDocumentService} from "../service/barn-document.service";
import {NzMessageService} from "ng-zorro-antd/message";

@Component({
  selector: 'app-my-document',
  templateUrl: './my-document.component.html',
  styleUrls: ['./my-document.component.less']
})
export class MyDocumentComponent implements OnInit {


  releaseYes = Constants.Enable.YES;
  releaseNo = Constants.Enable.NO

  validateForm = new FormGroup({
    documentTitle: new FormControl(''),
    productId: new FormControl(''),
    isRelease: new FormControl("")
  })

  documentList: BarnDocument[] = []
  allProducts: BarnProduct[] = []

  loading = false
  total = 0;
  current = 1;
  pageSize = 20
  pageSizes = [this.pageSize, 50, 100]


  constructor(private productService: ProductService, private documentService: BarnDocumentService, private msg: NzMessageService) {
  }

  ngOnInit(): void {
    const param = new BarnProduct();
    param.enable = Constants.Enable.YES
    this.productService.query(param).subscribe(value => {
      this.allProducts = value.data
    })
    this.search()

  }

  search() {
    this.loading = true;
    const param = this.validateForm.getRawValue();
    this.documentService.queryMyDocument(param, this.current, this.pageSize).subscribe(value => {
      this.loading = false
      this.current = value.current || 1;
      this.total = value.count || 1;
      this.documentList = value.data;
    }, () => this.loading = false)
  }

  resetForm() {
    this.validateForm.reset({isRelease: "", productId: ""})
  }

  delete(pid) {

    this.loading = true
    this.documentService.deleteById(pid).subscribe(() => {
      this.search()
      this.msg.success("保存成功!")
    }, () => this.loading = false)
  }
}
